package es.denisneuf.core.managers {

		/**
		* @author Daniel Alvaro
		* @email dalvaro@ifoline.com
		**/

		import flash.utils.Dictionary;
		import es.denisneuf.utils.Base64;
		import es.denisneuf.utils.LZW;
		
		//import flash.system.System;
		import mx.utils.ObjectUtil;	
	
		public class SuspendDataManager {
			
		public static var className:String = "SuspendDataManager";
		public static var packageName:String = "es.denisneuf.core.managers";
		public static var version:String = "1.0.00";
		public static var author:String = "Daniel Alvaro";			
			
		private static var variableSeparator:String = "&";
		private static var dataSeparator:String = "=";
		//private var maxLength:Number = 4096;
		//private var compress:Boolean = false;
		
		private var hmSuspendData:Dictionary;
	
			
		public function SuspendDataManager(value:String):void 
		{
			
			//trace ('SuspendDataManager (' + value + ')');
			
			//TODO compress string to store
			//test ratio
			var raw:String=value;
			//trace (raw.length);
			var compress:String = LZW.compress(raw);
			trace (raw.length + " :::: " +compress.length);
			
			var aux:String=Base64.decode(value);

			hmSuspendData = new Dictionary();
			
			if (value !== null) createSDMap(aux);
					 
		}
			
		internal function createSDMap (value:String):void {
	
			var itemsArr:Array = value.split(variableSeparator);
			
			var max:int = itemsArr.length;
	
			for (var i:int=0; i<max ;i++){
				
				var parValues:Array = itemsArr[i].split(dataSeparator);
				hmSuspendData[parValues[0]] = parValues[1];
			}
		}
	
		public function setData(key:String, value:String):void
		{
			//TODO check length
			trace ('setData' + value.length);
			hmSuspendData[key] = value;
		}		
			
		public function getData(key:String):String 
		{
			return hmSuspendData[key];
		}
	
	
	
		public function removeData (key:String):void
		{
			hmSuspendData[key] = null;
			delete hmSuspendData[key]; //removes the key
		}
	
		public function removeAllData():void
		{
			hmSuspendData = null; // maybe uncontrolled error not acess to dynamic properties of object
			
			//trace ("System.totalMemory: " + System.totalMemory);
			//System.gc();			
	        //trace ("System.totalMemory: " + System.totalMemory); 
			//delete hmSuspendData;
		}
		
		/*
		 * GET INFO
		 */
		
		
		public function getInfoMap():void{
			
			for (var key:Object in hmSuspendData)
			{
				trace (key + ' ___ ' + hmSuspendData[key]);	
			}	
	
		}
		
		public function getValuesMap():void
		{
			for each (var value:Object in hmSuspendData) {
			   trace(ObjectUtil.toString(value));
			}
		
		}
		
		
		
		

	}
}
